#!/bin/bash

set -o errexit
set -o pipefail
export LC_ALL=C

SYNC_DIR=$1
INPUT_DIR=$2
OUTPUT_DIR=$3
DATABASES_DIR=$4
SIMULATION_FILE=$5

PYTHON_BIN=/home/mixagol/local/Python-2.7/bin/python2.7
SRC_PATH=/home/mixagol/trunk/gene-class

while true; do
    fname=`$PYTHON_BIN ${SRC_PATH}/tools/schedule_files.py $SYNC_DIR $INPUT_DIR`
    if [[ "x$fname" = "x" ]]; then
        break
    fi
    
    zcat $INPUT_DIR/$fname \
        | $PYTHON_BIN ${SRC_PATH}/3_raw_matrix_nw/prepare_data.py \
            -G $DATABASES_DIR/genom.db \
            -g $DATABASES_DIR/gen.db   \
        | ${SRC_PATH}/3_raw_matrix_nw/nw_aligner_batch \
            -i $SIMULATION_FILE \
        | gzip \
        > $OUTPUT_DIR/.${fname}

    mv $OUTPUT_DIR/.${fname} $OUTPUT_DIR/${fname}
done
